<template>
  <div>
    <div>
      <we-button v-if="baseCode" @click="dialogFormVisible = true" type="primary" size="mini">
        <em class="el-icon-plus"></em> 新增
      </we-button>
      <we-button v-if="baseCode" @click="detailRow(currentRow)" type="primary" size="mini">
        <em class="el-icon-edit"></em> 修改
      </we-button>
      <we-button v-if="baseCode" @click="deleteRow"  type="primary" size="mini">
        <em class="el-icon-delete"></em> 删除
      </we-button>
      <span v-if="baseName">当前人员：【 {{baseName}} 】</span>
    </div>

    <!-- 表格 -->
    <el-table
      size="mini"
      :header-cell-style="rowClass"
      :data="tableData"
      height="116px"
      border
      highlight-current-row
      resizable
      ref="singleTable"
      @row-dblclick="detailRow"
      @current-change="handleCurrentChange"
      style="width: 100%; margin-top: 5px"
    >
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column
        prop="professional_dictText"
        width="150"
        :show-overflow-tooltip="true"
        label="专业技术资格名称"
      >
      </el-table-column>
      <el-table-column
        prop="professionalApproach_dictText"
        width="150"
        :show-overflow-tooltip="true"
        label="取得资格途径"
      >
      </el-table-column>
      <el-table-column
        prop="getDate"
        width="150"
        :show-overflow-tooltip="true"
        label="取得资格日期"
      >
      </el-table-column>
      <el-table-column
        prop="approvedBy"
        :show-overflow-tooltip="true"
        width="150"
        label="资格审批单位"
      >
      </el-table-column>
      <el-table-column
        prop="approvalNumber"
        :show-overflow-tooltip="true"
        width="150"
        label="资格审批文号"
      >
      </el-table-column>
      <el-table-column
        prop="appointment_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="聘任情况"
      >
      </el-table-column>
      <el-table-column
        prop="startDate"
        :show-overflow-tooltip="true"
        width="150"
        label="聘任起始日期"
      >
      </el-table-column>
      <el-table-column
        prop="endDate"
        :show-overflow-tooltip="true"
        width="150"
        label="聘任终止日期"
      >
      </el-table-column>
      <el-table-column
        prop="appointmentProfessional_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="聘任专业技术职务名称"
      >
      </el-table-column>
      <el-table-column
        prop="professionalLevel_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="聘任专业技术岗位等级"
      >
      </el-table-column>
      <el-table-column
        prop="appointmentCompany"
        :show-overflow-tooltip="true"
        width="150"
        label="聘任单位"
      >
      </el-table-column>
      <el-table-column
        prop="appointmentNumber"
        :show-overflow-tooltip="true"
        width="150"
        label="聘任文号"
      >
      </el-table-column>
      <el-table-column
        prop="ageWageRatio_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="享受教护龄工资比例"
      >
      </el-table-column>
      <el-table-column
        prop="expertiseType"
        :show-overflow-tooltip="true"
        width="150"
        label="有何专长"
      >
      </el-table-column>
      <!-- <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <we-button @click="detailRow(scope.row)" type="primary" size="mini"
            >详情</we-button
          >
          <we-button type="danger" @click="deleteRow(scope.row)" size="mini">删除</we-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="table_tools">
      <we-pagination
        :total="pagination.total"
        :page="pagination.pageNum"
        :pageSize="pagination.pageSize"
        @currentChange="currentChange($event)"></we-pagination>
    </div>
    <!-- 表格 -->

    <!-- 新增弹窗 -->
    <el-dialog
      title="专业技术职务子集"
      :visible.sync="dialogFormVisible"
      @close="handleOffDialog"
      append-to-body
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="170px"
        size="mini"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="专业技术资格名称" prop="professional">
              <el-cascader
                class="inputW"
                v-model="ruleForm.professional"
                :options="majorName"
                :props="{ expandTrigger: 'hover' ,value:'uuid',label:'name',children: 'children'}"
                @change="handleChange"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取得资格途径" prop="professionalApproach">
              <el-select
                class="inputW"
                v-model="ruleForm.professionalApproach"
                placeholder="请选择"
                clearable
              >
                <el-option v-for="item in qualificationApproach" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="取得资格日期" prop="getDate">
              <el-date-picker
                class="inputW"
                v-model="ruleForm.getDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资格审批单位" prop="approvedBy">
              <el-input
                class="inputW"
                v-model="ruleForm.approvedBy"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="资格审批文号" prop="approvalNumber">
              <el-input
                class="inputW"
                v-model="ruleForm.approvalNumber"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="聘任情况" prop="appointment">
              <el-select
                class="inputW"
                v-model="ruleForm.appointment"
                placeholder="请选择活动区域"
                clearable
              >
              <el-option v-for="item in appointment" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="聘任起始日期" prop="startDate">
              <el-date-picker
                class="inputW"
                v-model="ruleForm.startDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="聘任终止日期" prop="endDate">
              <el-date-picker
                class="inputW"
                v-model="ruleForm.endDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="聘任专业技术职务名称" prop="appointmentProfessional">
              <el-cascader
                class="inputW"
                v-model="ruleForm.appointmentProfessional"
                :options="appointmentName"
                :props="{ expandTrigger: 'hover' ,value:'uuid',label:'name',children: 'children'}"
                @change="handleChange"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="聘任专业技术岗位等级" prop="professionalLevel">
              <el-select
                class="inputW"
                v-model="ruleForm.professionalLevel"
                placeholder="请选择活动区域"
                clearable
              >
              <el-option v-for="item in appointLevel" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="聘任单位" prop="appointmentCompany">
              <el-input
                class="inputW"
                v-model="ruleForm.appointmentCompany"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="聘任文号" prop="appointmentNumber">
              <el-input
                class="inputW"
                v-model="ruleForm.appointmentNumber"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="享受教护龄工资比例" prop="ageWageRatio">
              <el-select
                class="inputW"
                v-model="ruleForm.ageWageRatio"
                placeholder="请选择活动区域"
                clearable
              >
              <el-option v-for="item in ageWageRatio" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有何专长" prop="expertiseType">
              <el-input
                class="inputW"
                v-model="ruleForm.expertiseType"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <we-button size="mini" @click="handleOffDialog"
          >取 消</we-button
        >
        <we-button v-if="!isUpdate" size="mini" type="primary" @click="submitForm('ruleForm')"
          >新 增</we-button
        >
        <we-button v-if="isUpdate" size="mini" type="primary" @click="updateForm('ruleForm')"
          >修 改</we-button
        >
      </div>
    </el-dialog>
    <!-- 新增弹窗 -->
  </div>
</template>

<script>
import WeButton from '@/components/WeButton'
import WePagination from '@/components/WePagination'
import { parseTree } from '@/utils/util'
import { getAction, postAction, deleteAction } from '@/api/manage'
import {bus} from "@/utils/eventBus";
import {opDictNew} from "@/api/apiUrl";
export default {
  components: {
    WeButton,
    WePagination
  },
  data () {
    return {
      pagination: {
        total: 10, // 总数据条数
        pageSize: 10, // 每页条数
        pageNum: 1 // 当前页
      }, // 列表分页数据
      tableData: [],
      dialogFormVisible: false,
      isUpdate: false,
      currentRow: null,
      ruleForm: {
        professional: '',
        professionalApproach: '',
        getDate: '',
        approvedBy: '',
        approvalNumber: '',
        appointment: '',
        startDate: '',
        endDate: '',
        appointmentProfessional: '',
        professionalLevel: '',
        appointmentCompany: '',
        appointmentNumber: '',
        ageWageRatio: '',
        expertiseType: '',
        uuidList:[],
      },
      rules: {
        professional: [
          {
            required: true,
            message: '请选择专业技术资格名称',
            trigger: 'change'
          }
        ],
        professionalApproach: [
          { required: true, message: '请选择取得资格途径', trigger: 'change' }
        ],
        getDate: [
          { required: true, message: '请选择取得资格日期', trigger: 'change' }
        ],
        approvedBy: [
          { required: true, message: '请填写资格审批单位', trigger: 'blur' }
        ],
        approvalNumber: [
          { required: true, message: '请填写资格审批文号', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '请选择聘任起始日期', trigger: 'change' }
        ],
        appointmentProfessional: [
          {
            required: true,
            message: '请选择聘任专业技术职务名称',
            trigger: 'change'
          }
        ],
        professionalLevel: [
          {
            required: true,
            message: '请选择聘任专业技术岗位等级',
            trigger: 'change'
          }
        ],
        appointmentNumber: [
          { required: true, message: '请填写聘任文号', trigger: 'blur' }
        ]
      },

      options: [],
      majorName: [], // 专业技术资格名称表
      appointmentName: [], // 聘任专业技术职务名称表
      qualificationApproach: [], // 取得资格途径
      appointment: [], // 聘任情况
      appointLevel: [], // 聘任专业技术岗位等级
      ageWageRatio: [], // 享受教护龄工资比例
      baseCode: '',
      baseName: '',
    }
  },
  watch: {
    dialogFormVisible () {
      if (this.dialogFormVisible === false) {
        if (this.isUpdate) {
          setTimeout(() => {
            this.isUpdate = false
            this.clearForm()
            if (this.$refs.ruleForm !== undefined) {
              this.$refs.ruleForm.resetFields()
            }
          }, 500)
        }
        if (this.$store.state.technical === true) {
          this.$store.commit('changeTechnical', false)
        }
      }
    },
    '$store.state.baseCode'(newval) {
      this.baseCode  = newval
      this.getList ()
    },
    '$store.state.baseName'(newval) {
      this.baseName  = newval
    },
    '$store.state.technical' (newval) {
      if (newval === true) {
        this.dialogFormVisible = true
        if (this.$store.state.updateSubset === true) {
          this.isUpdate = true
        } else {
          this.isUpdate = false
        }
        this.$store.commit('changeUpdateSubset', false)
        this.baseCodes = this.$store.state.baseCodes
        this.getMajorName()
        this.getAppointmentName()
        this.getQualificationApproach()
        this.getAppointment()
        this.getAppointLevel()
        this.getAgeWageRatio()
        this.getList()
      }
    }
  },
  methods: {
    clickSubset () {
      this.baseCode = this.$store.state.baseCode
      this.getMajorName()
      this.getAppointmentName()
      this.getQualificationApproach()
      this.getAppointment()
      this.getAppointLevel()
      this.getAgeWageRatio()
    },
    batchAdd(data){
      this.dialogFormVisible = true
      this.ruleForm.uuidList = data
    },
    /*
     * 新增
     */
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.ruleForm))
          data.professional = data.professional.pop()
          data.appointmentProfessional = data.appointmentProfessional.pop()
          data.baseCode = this.baseCode
          data.sysOrgCode = '测试部门'
          data.createUser = '测试员'
          if (this.$store.state.technical === true) {
            for (let i = 0; i < this.baseCodes.length; i++) {
              data.baseCode = this.baseCodes[i]
              postAction('/professionalPosition/add', data)
                .then(result => {
                  if (result.code === 200) {
                    this.dialogFormVisible = false
                    bus.$emit('handleDialog')
                    this.$message({
                      type: 'success',
                      message: '新增成功!'
                    })
                    this.clearForm()
                  } else {
                    this.$message({
                      type: 'waring',
                      message: result.msg
                    })
                  }
                })
            }
          } else {
            postAction('/professionalPosition/add', data)
              .then(result => {
                if (result.code === 200) {
                  this.getList()
                  this.dialogFormVisible = false
                  bus.$emit('handleDialog')
                  this.$message({
                    type: 'success',
                    message: '新增成功!'
                  })
                } else {
                  this.$message({
                    type: 'waring',
                    message: result.msg
                  })
                }
              })
          }
        } else {
          return false
        }
      })
    },
    handleCurrentChange(val) {
        this.currentRow = val;
    },
    /**
     * 删除
     */
    deleteRow (row) {
      if(!this.currentRow){
        this.$message("请选择要删除的数据！")
        return
      }
      this.$confirm('是否要删除这条数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteAction('/professionalPosition/delete', { uuids: [this.currentRow.uuid] })
            .then(result => {
              if (result.code === 200) {
                this.getList()
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
              } else {
                this.$message({
                  type: 'warning',
                  message: result.msg
                })
              }
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    /**
     * 修改
     */
    updateForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.ruleForm))
          if (typeof (this.ruleForm.professional) !== 'string') {
            data.professional = data.professional.pop()
          }
          if (typeof (this.ruleForm.appointmentProfessional) !== 'string') {
            data.appointmentProfessional = data.appointmentProfessional.pop()
          }
          data.updateUser = '修改员'
          postAction('/professionalPosition/update', data)
            .then(result => {
              if (result.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                bus.$emit('handleDialog')
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                })
              } else {
                this.$message({
                  type: 'waring',
                  message: result.msg
                })
              }
            })
        } else {
          return false
        }
      })
    },
    handleOffDialog() {
      this.dialogFormVisible = false
      bus.$emit('handleDialog')
    },
    /**
     * 查询
     */
    getList () {
      let self = this
      postAction('/professionalPosition/pageList', { baseCode: this.baseCode,size:this.pagination.pageSize, page:this.pagination.pageNum })
        .then(result => {
          if (result.code === 200) {
            if (!result.data.list.length && self.pagination.pageNum !== 1) {
              self.pagination.pageNum -= 1
              self.getList()
              return
            }
            self.pagination.total = result.data.total
            self.tableData = result.data.list
          }
        })
    },

    /*
     * 查询专业技术资格名称
     */
    getMajorName () {
       getAction('/opDictNew/findTreeByType?type=rs_zyjszwdm')
        .then(result => {
          if (result.code === 200) {
            this.majorName = result.data
            this.appointmentName = result.data
          }
        })
    },

    /*
     * 查询聘任专业技术职务名称
     */
    getAppointmentName () {
      // postAction('/appointmentProfessional/getAll2')
      //   .then(result => {
      //     if (result.code === 200) {
      //       this.appointmentName = parseTree(result.data)
      //     }
      //   })
    },

    /*
     * 获取取得资格途径字典
     */
    async getQualificationApproach () {
      const result = await   getAction(`${opDictNew.findByType}?type=rs_by`)
      this.qualificationApproach = result.data
    },

    /*
     * 获取聘任情况字典
     */
    async getAppointment () {
      const result = await  getAction('/opDictNew/findTreeByType?type=rs_cl')
      this.appointment = result.data
    },

    /*
     * 获取聘任专业技术岗位等级字典
     */
    async getAppointLevel () {
      const result = await getAction(`${opDictNew.findByType}?type=rs_gqgwjb`)
      this.appointLevel = result.data
    },

    /*
     * 获取享受教护龄工资比例字典
     */
    async getAgeWageRatio () {
      const result = await getAction(`${opDictNew.findByType}?type=rs_jhlgzbl`)
      this.ageWageRatio = result.data
    },

    /**
     * 详情
     */
    detailRow (row) {
      if(!row){
        this.$message("请选择要编辑的数据！")
        return
      }
      this.ruleForm = JSON.parse(JSON.stringify(row))
      this.isUpdate = true
      this.dialogFormVisible = true
    },

    /**
     * 当前页改变
     */
    currentChange (event) {
      this.pagination.pageNum = event
      this.getList()
    },

    /**
     * 清除form
     */
    clearForm () {
      this.ruleForm = {
        professional: '',
        professionalApproach: '',
        getDate: '',
        approvedBy: '',
        approvalNumber: '',
        appointment: '',
        startDate: '',
        endDate: '',
        appointmentProfessional: '',
        professionalLevel: '',
        appointmentCompany: '',
        appointmentNumber: '',
        ageWageRatio: '',
        expertiseType: ''
      }
    },

    rowClass ({ row, rowIndex }) {
      return 'background:rgba(248, 248, 249, 1);color:#000;'
    },
    handleChange () {},
    handleClose () {}
  }
}
</script>

<style scoped>

.table_tools {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
